package com.example.gaojt.androiddemo.statical.sdk;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.io.File;

/**
 * Created by gaojt on 7/7/17.
 */

public class LogInfoDB {

    private SQLiteDatabase mDb;

    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_MSG = "msg";
    private static final String TABLE_NAME = "log_info";

    public static final Object LOCK = new Object();

    public LogInfoDB(Context context, String processName) {
        File dbFile = context.getDatabasePath("zz_log_" + processName + ".db");
        if (!dbFile.getParentFile().exists()) {
            dbFile.getParentFile().mkdirs();
        }
        mDb = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
        mDb.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY, "
                + COLUMN_MSG + " TEXT" + ");");
    }

    protected long insert(ContentValues values) {
        return mDb.insert(TABLE_NAME, null, values);
    }

    protected int update(ContentValues values, String themeId) {
        return mDb.update(TABLE_NAME, values, COLUMN_ID + "=?", new String[] {themeId});
    }

    protected Cursor query(String[] columns, String selection, String[] selectionArgs) {
        return mDb.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null, null);
    }

    protected int delete(String whereClause, String[] whereArgs) {
        return mDb.delete(TABLE_NAME, whereClause, whereArgs);
    }

    protected void close() {
        mDb.close();
    }

}
